﻿using System.Globalization;
using System.Text;
using AutoMapper;
using EFI.Web.Framework.Controllers;
using OWE.Core.Domain;
using OWE.Service.Accounting;
using OWE.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OWE.Web.Controllers
{
    public class AccountController : Controller
    {

        private IAccountService _accountService;

        public AccountController()
        {

        }

        public AccountController(IAccountService accountService)
        {
            _accountService = accountService;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Create()
        {
            var model = new AccountModel();
            PopulateAccountTypes(model);
            return View(model);
        }
        [HttpPost]
        public ActionResult Create(AccountModel model)
        {
            PopulateAccountTypes(model);
            if (ModelState.IsValid)
            {
                Mapper.CreateMap<AccountModel, Account>();

                var account = Mapper.Map<AccountModel, Account>(model);

                account.CreatedOn = DateTime.Now;
                account.UpdatedOn = DateTime.Now;

                account.Name = model.Name;
                account.CurrencyId = 1;
                account.Code = model.Code;

                //model.AccountTypes.Where(f=)
                account.AccountType = model.AccountType;
                _accountService.InsertAccount(account);
                return RedirectToAction("List");
            }
            return View(model);
        }

        public ActionResult List()
        {
            var model = _accountService.GetAllAccountsWithDebitCredits().EntityDataList;
            return View(model);
        }

        public ActionResult ListJson()
        {
            var model = _accountService.GetAllAccountsWithDebitCredits().EntityDataList;
            var sb = new StringBuilder();
            sb.Append("{\"data\": [");
            for (int i = 0; i < model.Count; i++)
            {
                var a = model[i];
                sb.AppendFormat("[\"<input type=\\\"checkbox\\\" name=\\\"id[]\\\" value=\\\"{0}\\\">\", {0}, \"{1}\", \"{2}\", \"{3}\", \"{4}\", {5}, \"{6}\", \"<a href=\\\"javascript:;\\\" class=\\\"btn btn-xs default\\\"><i class=\\\"fa fa-search\\\"><\\/i> View<\\/a>\"],", a.Id, a.Code, a.Name, a.AccountTypeName, a.Debit, a.Credit, a.Balance);
                if(i >= 10)
                {
                    break;
                }
            }
            //Columns
            sb.Append("], \"draw\": 2,\"recordsTotal\": 178, \"recordsFiltered\": 178}");
            return Content(sb.ToString());
        }

        

        [NonAction]
        private void PopulateAccountTypes(AccountModel model)
        {
            var accountTypes = _accountService.GetAllAccountTypes().EntityDataList;
            model.AccountTypes = new List<SelectListItem>();

            foreach (var accountType in accountTypes)
            {
                model.AccountTypes.Add(new SelectListItem { Text = accountType.AccountTypeName, Value = accountType.Id.ToString(CultureInfo.InvariantCulture) });
            }
        }
    }
}
